package com.dhcc.bpm.modules.system.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.entity.Menu;
import com.dhcc.bpm.modules.system.entity.TopMenu;
import com.dhcc.bpm.modules.system.service.IMenuService;
import com.dhcc.bpm.modules.system.service.ITopMenuService;
import com.dhcc.bpm.modules.system.vo.CheckedTreeVO;
import com.dhcc.bpm.modules.system.vo.GrantTreeVO;
import com.dhcc.bpm.modules.system.vo.MenuVO;
import com.dhcc.bpm.modules.system.wrapper.MenuWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.MENU_CACHE;
/**
 * @ClassName MenuController
 * @Description 菜单控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/menu")
@Api(value = "菜单", tags = "菜单")
public class MenuController extends BladeController {

	private IMenuService menuService;
	private ITopMenuService topMenuService;
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 10:04 2020/2/22 0022
	 * @param menu
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.MenuVO>
	 **/
	@GetMapping("/detail")
	@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入menu")
	@ApiLog("查询菜单详情")
	@Log(title = "查询菜单详情", businessType = BusinessType.SELECT)
	public R<MenuVO> detail(Menu menu) {
		Menu detail = menuService.getOne(Condition.getQueryWrapper(menu));
		return R.data(MenuWrapper.build().entityVO(detail));
	}

	/**
	 * @Description 列表
	 * @Author sjm
	 * @Date 10:04 2020/2/22 0022
	 * @param menu
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.MenuVO>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
	})
	@PreAuth("hasAnyRole('administrator', 'user','test')")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入menu")
	@ApiLog("查询菜单列表")
	@Log(title = "查询菜单列表", businessType = BusinessType.SELECT)
	public R<List<MenuVO>> list(@ApiIgnore @RequestParam Map<String, Object> menu) {
		List<Menu> list = menuService.list(Condition.getQueryWrapper(menu, Menu.class).lambda().orderByAsc(Menu::getSort));
		return R.data(MenuWrapper.build().listNodeVO(list));
	}

	/**
	 * @Description 列表
	 * @Author sjm
	 * @Date 10:04 2020/2/22 0022
	 * @param menu
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.MenuVO>>
	 **/
	@GetMapping("/menu-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
	})
//	@PreAuth(AuthConstant.PERMISSION_ALL)
	@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入menu")
	@ApiLog("查询菜单列表")
	@Log(title = "查询菜单列表", businessType = BusinessType.SELECT)
	public R<List<MenuVO>> menuList(@ApiIgnore @RequestParam Map<String, Object> menu) {
		List<Menu> list = menuService.list(Condition.getQueryWrapper(menu, Menu.class).lambda().eq(Menu::getAlias, "menu").orderByAsc(Menu::getSort));
		return R.data(MenuWrapper.build().listNodeVO(list));
	}

	/**
	 * @Description 新增或修改
	 * @Author sjm
	 * @Date 10:04 2020/2/22 0022
	 * @param menu
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@CacheEvict(cacheNames = {MENU_CACHE}, allEntries = true)
	@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增或修改", notes = "传入menu")
	@ApiLog("新增或修改菜单")
	@Log(title = "菜单", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Menu menu) {
		menu.setExpSeq(stringRedisTemplate.opsForValue().get("expSeq"));
		return R.status(menuService.submit(menu));
	}


	/**
	 * @Description 删除
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@CacheEvict(cacheNames = {MENU_CACHE}, allEntries = true)
	@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("删除菜单")
	@Log(title = "菜单", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(menuService.removeMenu(ids));
	}

	/**
	 * @Description 前端菜单数据
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param user
	 * @param topMenuId
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.MenuVO>>
	 **/
	@GetMapping("/routes")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "前端菜单数据", notes = "前端菜单数据")
	@ApiLog("查询前端菜单数据")
	@Log(title = "查询前端菜单数据", businessType = BusinessType.SELECT)
	public R<List<MenuVO>> routes(BladeUser user, Long topMenuId) {
		List<MenuVO> list = menuService.routes((user == null) ? null : user.getRoleId(), topMenuId);
		return R.data(list);
	}

	/**
	 * @Description 前端菜单数据
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param user
	 * @param topMenuId
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.MenuVO>>
	 **/
	@GetMapping("/routes-ext")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "前端菜单数据", notes = "前端菜单数据")
	@ApiLog("查询前端菜单数据")
	@Log(title = "查询前端菜单数据", businessType = BusinessType.SELECT)
	public R<List<MenuVO>> routesExt(BladeUser user, Long topMenuId) {
		List<MenuVO> list = menuService.routesExt(user.getRoleId(), topMenuId);
		return R.data(list);
	}

	/**
	 * @Description 前端按钮数据
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.MenuVO>>
	 **/
	@GetMapping("/buttons")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "前端按钮数据", notes = "前端按钮数据")
	@ApiLog("查询前端按钮数据")
	@Log(title = "前端按钮数据", businessType = BusinessType.SELECT)
	public R<List<MenuVO>> buttons(BladeUser user) {
		List<MenuVO> list = menuService.buttons(user.getRoleId());
		return R.data(list);
	}

	/**
	 * @Description 获取菜单树形结构
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.MenuVO>>
	 **/
	@GetMapping("/tree")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	@ApiLog("获取菜单树形结构")
	@Log(title = "获取菜单树形结构", businessType = BusinessType.SELECT)
	public R<List<MenuVO>> tree() {
		List<MenuVO> tree = menuService.tree();
		return R.data(tree);
	}

	/**
	 * @Description 获取权限分配树形结构
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.GrantTreeVO>
	 **/
	@GetMapping("/grant-tree")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "权限分配树形结构", notes = "权限分配树形结构")
	@ApiLog("权限分配树形结构")
	@Log(title = "权限分配树形结构", businessType = BusinessType.SELECT)
	public R<GrantTreeVO> grantTree(BladeUser user) {
		GrantTreeVO vo = new GrantTreeVO();
		vo.setMenu(menuService.grantTree(user));
		vo.setDataScope(menuService.grantDataScopeTree(user));
		vo.setApiScope(menuService.grantApiScopeTree(user));
		return R.data(vo);
	}

	/**
	 * @Description 获取权限分配树形结构
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param roleIds
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.CheckedTreeVO>
	 **/
	@GetMapping("/role-tree-keys")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "角色所分配的树", notes = "角色所分配的树")
	@ApiLog("获取权限分配树形结构")
	@Log(title = "获取权限分配树形结构", businessType = BusinessType.SELECT)
	public R<CheckedTreeVO> roleTreeKeys(String roleIds) {
		CheckedTreeVO vo = new CheckedTreeVO();
		vo.setMenu(menuService.roleTreeKeys(roleIds));
		vo.setDataScope(menuService.dataScopeTreeKeys(roleIds));
		vo.setApiScope(menuService.apiScopeTreeKeys(roleIds));
		return R.data(vo);
	}

	/**
	 * @Description 获取顶部菜单树形结构
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.GrantTreeVO>
	 **/
	@GetMapping("/grant-top-tree")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "顶部菜单树形结构", notes = "顶部菜单树形结构")
	@ApiLog("获取顶部菜单树形结构")
	@Log(title = "获取顶部菜单树形结构", businessType = BusinessType.SELECT)
	public R<GrantTreeVO> grantTopTree(BladeUser user) {
		GrantTreeVO vo = new GrantTreeVO();
		vo.setMenu(menuService.grantTopTree(user));
		return R.data(vo);
	}

	/**
	 * @Description 获取顶部菜单树形结构
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param topMenuIds
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.CheckedTreeVO>
	 **/
	@GetMapping("/top-tree-keys")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "顶部菜单所分配的树", notes = "顶部菜单所分配的树")
	@ApiLog("获取顶部菜单树形结构")
	@Log(title = "获取顶部菜单树形结构", businessType = BusinessType.SELECT)
	public R<CheckedTreeVO> topTreeKeys(String topMenuIds) {
		CheckedTreeVO vo = new CheckedTreeVO();
		vo.setTopMenu(menuService.topTreeKeys(topMenuIds));
		return R.data(vo);
	}

	/**
	 * @Description 顶部菜单数据
	 * @Author sjm
	 * @Date 10:05 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.entity.TopMenu>>
	 **/
	@GetMapping("/top-menu")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "顶部菜单数据", notes = "顶部菜单数据")
	@ApiLog("顶部菜单数据")
	@Log(title = "顶部菜单数据", businessType = BusinessType.SELECT)
	public R<List<TopMenu>> topMenu(BladeUser user) {
		if (Func.isEmpty(user)) {
			return null;
		}
		List<TopMenu> list = topMenuService.list();
		return R.data(list);
	}

	/**
	 * @Description 获取配置的角色权限
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<java.util.List<org.springblade.core.tool.support.Kv>>
	 **/
	@GetMapping("auth-routes")
	@ApiOperationSupport(order = 15)
	@ApiOperation(value = "菜单的角色权限")
	@ApiLog("菜单的角色权限")
	@Log(title = "菜单的角色权限", businessType = BusinessType.SELECT)
	public R<List<Kv>> authRoutes(BladeUser user) {
		if (Func.isEmpty(user)) {
			return null;
		}
		return R.data(menuService.authRoutes(user));
	}

}
